package cn.bdqn.controller;

import cn.bdqn.domain.Delivery;
import cn.bdqn.service.DeliveryService;
import cn.bdqn.utils.result.Page;
import cn.bdqn.utils.result.Result;
import cn.bdqn.utils.result.ResultGenerator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 派单控制层
 * @author dkj
 */
@RestController
public class DeliveryController {

    /**
     * 创建日志记录器
     */
    Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private DeliveryService deliveryService;


    /**
     * 根据id查询
     * @param id
     * @return
     */
    @GetMapping("/delivery/query/{id}")
    public Result queryByPrimaryKey(@PathVariable("id") Integer id){

        try {
            logger.info("进入方法了,参数：id={}",id);
            //根据id查询
            Delivery delivery = deliveryService.queryByPrimaryKey(id);
            //返回结果
            return ResultGenerator.genSuccessResult(delivery);
        }catch (Exception e){
            e.printStackTrace();
            logger.error("报错了:"+e.getMessage());
            return ResultGenerator.genFailResult("查询失败");
        }
    }


    /**
     * 新增
     * @param delivery
     * @return
     */
    @PostMapping("/delivery/save/")
    public Result saveDelivery(@RequestBody Delivery delivery){

        try {
            logger.info("进入方法了，参数：delivery={}",delivery);
            //新增
            Delivery delivery1 = deliveryService.saveDelivery(delivery);
            //返回结果
            return ResultGenerator.genSuccessResult(delivery1);
        }catch (Exception e){
            e.printStackTrace();
            logger.error("报错了："+e.getMessage());
            return ResultGenerator.genFailResult("添加失败");
        }
    }


    /**
     * 根据订单号模糊查询
     * @param currentPage
     * @param pageSize
     * @param sendFreightNumber
     * @return
     */
    @GetMapping("/delivery/querySendFreightNumber/{currentPage}/{pageSize}/{sendFreightNumber}")
    public Result queryBySendFreightId(@PathVariable("currentPage") Integer currentPage,
                                       @PathVariable("pageSize") Integer pageSize,
                                       @PathVariable("sendFreightNumber") String sendFreightNumber){
        try {
            logger.info("进入了方法，参数：currentPage={},pageSize={},sendFreightNumber={}",currentPage,pageSize,sendFreightNumber);
            //分页查询
            Page page = deliveryService.queryBySendFreightId(currentPage, pageSize, sendFreightNumber);
            //返回结果
            return ResultGenerator.genSuccessListResult(page.getMenuList(),page.getTotalRecord());
        }catch (Exception e){
            e.printStackTrace();
            logger.error("报错了："+e.getMessage());
            return ResultGenerator.genFailResult("查询失败");
        }
    }


    /**
     * 修改
     * @param delivery
     */

    @PutMapping("/delivery/update/")
    public Result updateDelivery(@RequestBody Delivery delivery){

        try {
            logger.info("进入了方法，参数：delivery={}",delivery);
            //修改
            deliveryService.updateDelivery(delivery);
            return ResultGenerator.genSuccessResult();
        }catch (Exception e){
            e.printStackTrace();
            logger.error("报错了："+e.getMessage());
            return ResultGenerator.genFailResult("修改失败");
        }
    }

}
